package by.demchenko.sample.webapp.page.home.cards;

import by.demchenko.sample.datamodel.Person;
import by.demchenko.sample.webapp.app.BasicAuthenticationSession;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;

public class ProfileCardPanel extends Panel {

    public ProfileCardPanel(String id) {
        super(id);

        Person user = BasicAuthenticationSession.get().getUser();

        Form<Person> form = new Form<>("form", new CompoundPropertyModel<>(user));
        form.add(createTextField(user, "firstName"));
        form.add(createTextField(user, "lastName"));

        add(form);
    }

    private TextField<String> createTextField(Person person, String fieldName) {
        final TextField<String> tField = new TextField<>(fieldName);
        tField.add(new PropertyValidator<>());
        tField.setLabel(new ResourceModel("person." + fieldName));
        return tField;
    }
}
